<?php

/*

Auctions (auctions.php)

*/

$page_title = "Auctions";
$rank_check = 1;
include "header.inc.php";

$findEnded = mysql_query("SELECT * FROM auctions2 WHERE end_time <= '$timestamp' AND game = '$game'");
while ($getEnded = mysql_fetch_array($findEnded))
{
	if ($getEnded[high_bidder])
	{
		points($game, "$getEnded[owner]", "$getEnded[high_bid]");
		$findItem = fetch("SELECT parts,id FROM items2 WHERE item_name = '$getEnded[item_name]' AND game = '$game'");
		$getWinner = fetch("SELECT id FROM members2 WHERE username = '$getEnded[high_bidder]' AND game = '$game'");
		mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$getWinner[id]','$findItem[id]','$findItem[parts]','$game')");
		systemmessage("$game", "auctions.php?game=$game","$getWinner[id]","You have won the auction on the $getEnded[item_name] for $getEnded[high_bid]!");
		$getAuctioner = fetch("SELECT id FROM members2 WHERE username = '$getEnded[owner]' AND game = '$game'");
		systemmessage("$game", "auctions.php?game=$game","$getAuctioner[id]","Your auction of the $getEnded[item_name] has ended, and you have been awarded with $getEnded[high_bid] $pointsVar!");
	}
	if (!$getEnded[high_bidder])
	{
		$findItem = fetch("SELECT parts,id FROM items2 WHERE item_name = '$getEnded[item_name]' AND game = '$game'");
		$getWinner = fetch("SELECT id FROM members2 WHERE username = '$getEnded[owner]' AND game = '$game'");
		mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$getWinner[id]','$findItem[id]','$findItem[parts]','$game')");
		systemmessage("$game", "auctions.php?game=$game","$getWinner[id]","Your auction of the $getEnded[item_name] got no bids, your item has been returned to you.");
	}
	mysql_query("DELETE FROM auctions2 WHERE id = '$getEnded[id]' AND game = '$game'");
}

print "$openHTML";

ECHO <<<END

<p align=center><font size=+2><a href=auctions.php?game=$game&page=search&search_by=my_auctions>My Auctions</a> | <a href=auctions_create.php?game=$game>Create an Auction</a></font></p>

<P><FORM ACTION="auctions.php?game=$game&page=search" METHOD=POST>
   <CENTER><TABLE BGCOLOR="$tableOutline" CELLSPACING=1 CELLPADDING=0 WIDTH=400>
      <TR BGCOLOR="$topAndBottomBG">
         <TD HEIGHT=45>
            <CENTER><FONT SIZE="+3" COLOR="$topAndBottomText"><B>--Auctions--</B></FONT></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$maincellColor">
         <TD HEIGHT=25>
            <CENTER><FONT SIZE="-1">Order by: </FONT><A HREF="auctions.php?game=$game&page=search&orderby=ending_soonest"><FONT SIZE="-1">Ending Soonest</FONT></A><FONT SIZE="-1">, </FONT><A HREF="auctions.php?game=$game&page=search&orderby=ending_last"><FONT SIZE="-1">Ending Last</FONT></A><FONT SIZE="-1">, </FONT><A HREF="auctions.php?game=$game&page=search&orderby=highest_price"><FONT SIZE="-1">Highest
            Price</FONT></A><FONT SIZE="-1">, </FONT><A HREF="auctions.php?game=$game&page=search&orderby=lowest_price"><FONT SIZE="-1">Lowest
            Price</FONT></A></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$reallyLight">
         <TD HEIGHT=25>
            <CENTER><FONT SIZE="-1">Search where:<SELECT NAME=search_by>
               <OPTION VALUE="item_name_contains">Item Name Contains
               <OPTION VALUE="item_name_is">Item Name IS
               <OPTION VALUE="owner">Owner IS
            </SELECT></FONT></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$maincellColor">
         <TD HEIGHT=25>
            <CENTER><FONT SIZE="-1">The words:
            <INPUT TYPE=text NAME=search_for VALUE="" SIZE=22></FONT></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$reallyLight">
         <TD>
            <CENTER><INPUT TYPE=submit NAME=Submit VALUE="Search Auctions!"></CENTER>
         </TD>
      </TR>
   </TABLE>
   </CENTER>
</FORM></P>

<CENTER><TABLE BGCOLOR="$tableOutline" CELLSPACING=1 CELLPADDING=0 WIDTH="90%">
   <TR BGCOLOR="$topAndBottomBG">
      <TD COLSPAN=4 HEIGHT=45>
         <CENTER><FONT SIZE="+2" COLOR="$topAndBottomText"><B>--Listing
         Auctions--</B></FONT></CENTER>
      </TD>
   </TR>
END;

if ($page == "search")
{
	if ($orderby == "ending_soonest") { $order_by = " ORDER BY end_time ASC"; $limit = " LIMIT 20"; }
	if ($orderby == "ending_last") { $order_by = " ORDER BY end_time DESC"; $limit = " LIMIT 50"; }
	if ($orderby == "highest_price") { $order_by = " ORDER BY high_bid DESC"; $limit = " LIMIT 50"; }
	if ($orderby == "lowest_price") { $order_by = " ORDER BY high_bid ASC"; $limit = " LIMIT 50"; }
	if ($search_by == "item_name_contains")
	{
		$search_query = mysql_query("SELECT * FROM auctions2 WHERE item_name LIKE '%$search_for%' AND game = '$game' ORDER BY end_time ASC LIMIT 50");
	}
	if ($search_by == "item_name_is")
	{
		$search_query = mysql_query("SELECT * FROM auctions2 WHERE item_name = '$search_for' AND game = '$game' ORDER BY end_time ASC LIMIT 50");
	}
	if ($search_by == "owner")
	{
		$search_query = mysql_query("SELECT * FROM auctions2 WHERE owner = '$search_for' AND game = '$game' ORDER BY end_time ASC");
	}
	if ($search_by == "my_auctions")
	{
		$search_query = mysql_query("SELECT * FROM auctions2 WHERE owner = '$username' AND game = '$game' ORDER BY end_time ASC");
	}
}

if (!$search_query)
{
	if (!$orderby) { $order_by = " ORDER BY end_time ASC"; $limit = " LIMIT 20"; }
	$search_query = mysql_query("SELECT * FROM auctions2 WHERE game = '$game'$order_by$limit");
}

while ($get_query = mysql_fetch_array($search_query))
{
	$time_left = $get_query[end_time] - $timestamp;
	$seconds = $time_left % 60;
	$minutes = round(($time_left / 60) % 60);
	$hours = round(($time_left / 60) / 60) % 60 - 1;
	$time_left = "$seconds Sec.";
	if ($minutes >= 1) { $time_left = "$minutes Min. $time_left"; }
	if ($hours >= 1) { $time_left = "$hours Hours $time_left"; }

	if ($get_query[buy_now_price] > 0)
	{
		$buyNow = "<br><font size=-1><a href=auctions_buynow.php?game=$game&id=$get_query[id]>Buy now for $get_query[buy_now_price] $pointsVar!</a></font>";
	}
	else
	{
		$buyNow = "";
	}
	
	if (!$get_query[high_bidder]) { $get_query[high_bidder] = "<i>no one</i>"; } else { $get_query[high_bidder] = "<a href=user_profile.php?game=$game&user=$get_query[high_bidder]>$get_query[high_bidder]</a>"; }

	$findItem = fetch("SELECT id FROM items2 WHERE item_name = '$get_query[item_name]'");
	
	$cellcolor = alternate($cellcolor,"$middleBG","#FFFFFF");
	print "
	<TR BGCOLOR=\"$cellcolor\">
      <TD HEIGHT=35 width=35>
         <img src=$base_url/images/user_images/opg_$game/items/item_$findItem[id].gif height=35 width=35>
      </TD>
      <TD HEIGHT=35>
         <CENTER><a href=auctions_bid.php?game=$game&id=$get_query[id]><b>$get_query[item_name]</b></a><br>
         <FONT SIZE=\"-1\"><I>($get_query[bids] bids)</I></FONT></CENTER>
      </TD>
      <TD HEIGHT=35 width=200>
         <CENTER><FONT SIZE=\"-1\">$get_query[high_bidder]<br>$get_query[high_bid] $pointsVar
         $buyNow</FONT></CENTER>
      </TD>
      <TD HEIGHT=35>
         <CENTER><font size=-1>$time_left<br><font size=-1>Auctioned by: <a href=user_profile.php?game=$game&user=$get_query[owner]>$get_query[owner]</a></font></font></CENTER>
      </TD>
   </TR>
	";
}

print "</TABLE></CENTER>";

print "$closeHTML";
?>
